package com.pzhu.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pzhu.model.dto.CouponDto;
import com.pzhu.model.entity.Coupon;
import com.pzhu.model.help.ResponseResult;
import com.pzhu.service.CouponService;
import com.pzhu.mapper.CouponMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Administrator
* @description 针对表【hs_coupon(优惠卷表)】的数据库操作Service实现
* @createDate 2024-01-02 19:36:33
*/
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon>
    implements CouponService{

    @Autowired
    private CouponMapper couponMapper;


    @Override
    public ResponseResult findFit(CouponDto couponDto) {

        LambdaQueryWrapper<Coupon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(couponDto.getThaliId() != -1,Coupon::getThaliId,couponDto.getThaliId())
                .eq(Coupon::getIsUsed,false)
                .eq(Coupon::getUserId,couponDto.getUserId())
                .gt(Coupon::getEndTime,new Date())
                .le(couponDto.getPrice().intValue() != 0,Coupon::getConsumePass,couponDto.getPrice());
        List<Coupon> couponList = couponMapper.selectList(queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("couponList",couponList);

        return ResponseResult.ok(map);
    }

    @Override
    public ResponseResult findCount(Long id) {
        LambdaQueryWrapper<Coupon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Coupon::getUserId,id);
        Integer count = couponMapper.selectCount(queryWrapper);

        Map<String,Object> map = new HashMap<>();
        map.put("count",count);

        return ResponseResult.ok(map);

    }

}




